package com.czy.owner.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.OrderCouponsListModel;
import com.czy.owner.utils.StringUtils;
import com.czy.owner.utils.TimeUtils;


import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by hedepu on 2017-09-08
 */

public class OrderCouponAdapter extends BaseRecyclerAdapter<OrderCouponsListModel, OrderCouponAdapter.OrderCouponHolder> {

    private Context ctx;
    private String money = "";
    private int isPosition = -1;

    public OrderCouponAdapter(Context context, List<OrderCouponsListModel> data) {
        super(context, data);
        this.ctx = context;
    }

    @Override
    public void onBindViewHolder(OrderCouponHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        holder.bindData(data.get(position), position);

    }

    public void setIsPosition(int isPosition) {
        this.isPosition = isPosition;
        notifyDataSetChanged();
    }

    public int getIsPosition() {
        return isPosition;
    }

    @Override
    public OrderCouponHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new OrderCouponHolder(layoutInflater.inflate(R.layout.item_pick_coupon, parent, false));
    }

    public class OrderCouponHolder extends BaseRecyclerAdapter.ViewHolder {
        //        2017-10-13 wangyang新增 优惠券样式修改
        @BindView(R.id.ll_bg)
        LinearLayout llBg;
        @BindView(R.id.ll_xuxian)
        LinearLayout llXuxian;
        @BindView(R.id.tv_use_store)
        TextView tvUseStore;
        //////////////////////
        @BindView(R.id.tv_item_coupon_name)
        TextView tvItemCouponName;
        @BindView(R.id.img_coupons_type)
        ImageView imgCouponsType;
        @BindView(R.id.rel_coupons_type)
        RelativeLayout relCouponsType;
        @BindView(R.id.tv_used_description)
        TextView tvUsedDescription;
        @BindView(R.id.tv_used_data)
        TextView tvUsedData;
        @BindView(R.id.tv_item_time)
        TextView tvItemTime;
        @BindView(R.id.ll_time)
        LinearLayout llTime;
        @BindView(R.id.cb_item_picked)
        CheckBox cbItemPicked;
        @BindView(R.id.tv_used_money)
        TextView tvUsedMoney;
        @BindView(R.id.tv_used_yuan)
        TextView tvUsedYuan;
        @BindView(R.id.ll_money)
        LinearLayout llMoney;
        @BindView(R.id.tv_used_case)
        TextView tvUsedCase;
        private int yellowColor = Color.parseColor("#FDCD54");
        private int themeColor = Color.parseColor("#09c0c7");

        public OrderCouponHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bindData(OrderCouponsListModel model, int position) {
            cbItemPicked.setClickable(false);
            if (StringUtils.isEmpty(model.getStoreId()) || Double.parseDouble(model.getStoreId()) == 0) {
                llBg.setBackgroundResource(R.mipmap.coupons_blue_bg);
                llXuxian.setBackgroundResource(R.mipmap.coupons_xuxian_blue_bg);
                tvUseStore.setBackgroundResource(R.drawable.selector_user_store_system_coupons_bg);
                tvUseStore.setTextColor(themeColor);
                tvUseStore.setText("限平台商城使用");
                tvUsedData.setTextColor(themeColor);
                tvItemTime.setTextColor(themeColor);
                tvUsedMoney.setTextColor(themeColor);
                tvUsedYuan.setTextColor(themeColor);
                tvItemCouponName.setTextColor(themeColor);

            } else {
                llBg.setBackgroundResource(R.mipmap.coupons_red_bg);
                llXuxian.setBackgroundResource(R.mipmap.coupons_xuxian_red_bg);
                tvUseStore.setBackgroundResource(R.drawable.selector_user_store_bg);
                tvUseStore.setTextColor(yellowColor);
                tvUseStore.setText("限" + UserHelper.getInstance().getOwnerInfoModel(mContext).getStoreName() + "使用");
                tvUsedData.setTextColor(yellowColor);
                tvItemTime.setTextColor(yellowColor);
                tvUsedMoney.setTextColor(yellowColor);
                tvUsedYuan.setTextColor(yellowColor);
                tvItemCouponName.setTextColor(yellowColor);

            }

            if (position == isPosition) {
                cbItemPicked.setChecked(true);
            } else {
                cbItemPicked.setChecked(false);
            }
            tvItemCouponName.setText(model.getCouponsName());
            tvUsedDescription.setText(model.getDescription());
//            if ("限用".equals(model.getUseTypeName())) {
//                imgCouponsType.setBackgroundResource(R.mipmap.bg_coupons_common_yellow);
//            } else {
//
//            }
            if (model.isUseType()) {
                imgCouponsType.setImageResource(R.mipmap.bg_coupons_common_yellow);
            } else {
                imgCouponsType.setImageResource(R.mipmap.bg_coupons_unusered_red);
            }
            tvUsedMoney.setText(model.getDenomination());
            if (StringUtils.isEmpty(model.getMinimum()) || Double.parseDouble(model.getMinimum()) == 0) {
                tvUsedCase.setText("无门槛");

            } else {
                tvUsedCase.setText("满" + model.getMinimum() + "元可用");
            }
            if ("0".equals(data.get(position).getEndTime())) {
                tvItemTime.setText("永久有效");
            } else {
                tvItemTime.setText(TimeUtils.timeStamp2Date(data.get(position).getStartTime(), "yyyy.MM.dd") + "-" + TimeUtils.timeStamp2Date(data.get(position).getEndTime(), "yyyy.MM.dd"));
            }
        }

    }
}

